package com.x.chalk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.x.chalk.dao.MoodLikeDao;
import com.x.chalk.entity.MoodLike;
import com.x.chalk.service.intf.MoodLikeService;
import com.x.chalk.service.intf.MoodService;
import com.x.chalk.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author FSir
 * @create 2022/10/4 16:18
 */
@Service
public class MoodLikeServiceImpl implements MoodLikeService {
    @Autowired
    private MoodLikeDao moodLikeDao;
    @Autowired
    private MoodService moodService;

    @Override
    @Transactional
    public R like(String token, Long mid) {
        long uid = Long.parseLong(token);
        LambdaQueryWrapper<MoodLike> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MoodLike::getUid,uid);
        queryWrapper.eq(MoodLike::getMoodId,mid);
        MoodLike moodLike = moodLikeDao.selectOne(queryWrapper);
        //未点赞
        if (moodLike == null) {
            //增加点赞记录
            moodLikeDao.insert(new MoodLike(uid, mid));
            //增加点赞数量
            moodService.updateLikeNum(1,mid);
        }else {
            //删除点赞记录
            moodLikeDao.delete(queryWrapper);
            moodService.updateLikeNum(-1,mid);
        }
        return R.ok();
    }
}
